package com.gaga.common.utils;

import com.gaga.common.result.AudioDeviceResult;
import net.beadsproject.beads.core.AudioContext;
import net.beadsproject.beads.data.Sample;
import net.beadsproject.beads.data.SampleManager;

import javax.sound.sampled.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liujia
 * @version 1.0
 * @date 2025/3/30-15:32
 */
public class DeviceInfoUtils {

    /**
     * 获取音频设备列表
     *
     * @return
     */
    public static List<AudioDeviceResult> getAudioDeviceList() {
        List<AudioDeviceResult> deviceList = new ArrayList<>();
        Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
        for (Mixer.Info mixerInfo : mixerInfos) {
            Mixer mixer = AudioSystem.getMixer(mixerInfo);
            // 检查是否支持播放功能
            if (mixer.isLineSupported(new Line.Info(SourceDataLine.class)) || mixer.isLineSupported(new Line.Info(Clip.class))) {
                AudioDeviceResult audioDeviceResult = new AudioDeviceResult();
                audioDeviceResult.setDeviceId(mixerInfo.getName());
                audioDeviceResult.setName(mixerInfo.getName());
                deviceList.add(audioDeviceResult);
            }
        }
        return deviceList;
    }

    public static Mixer.Info getTargetAudioDevice(String deviceId, boolean isOutput) {
        // 1. 寻找目标设备
        for (Mixer.Info info : AudioSystem.getMixerInfo()) {
            Mixer mixer = AudioSystem.getMixer(info);
            Line.Info lineInfo = isOutput ?
                    new DataLine.Info(Clip.class, null) :
                    new DataLine.Info(TargetDataLine.class, null);
            if (mixer.isLineSupported(lineInfo) && info.getName().contains(deviceId)) {
                return info;
            }
        }
        return null;
    }

    public static int getMixerIndex(String deviceId) {
        int resultIndex = -1;
        Mixer.Info[] mixerinfos = AudioSystem.getMixerInfo();
        for (int i = 0; i < mixerinfos.length; i++) {
            Mixer mixer = AudioSystem.getMixer(mixerinfos[i]);
            if (mixer.isLineSupported(new Line.Info(SourceDataLine.class)) || mixer.isLineSupported(new Line.Info(Clip.class))) {
                if (mixerinfos[i].getName().contains(deviceId)) {
                    resultIndex = i;
                    break;
                }
            }
        }
        return resultIndex;
    }

    public static int getAudioDuration(String filePath) {
        try {
            // 初始化音频上下文
            AudioContext ac = new AudioContext();

            // 使用SampleManager加载音频文件
            Sample sample = SampleManager.sample(filePath);

            if (sample != null) {
                double duration = sample.getLength() / 1000;
                return (int) Math.round(duration);
            } else {
                System.err.println("无法加载音频文件: " + filePath);
                return 5;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 5;
        }
    }

    public static void main(String[] args) {
        System.out.println(getAudioDeviceList());
    }
}
